<?php

namespace App\Admin\Controllers\User;

use App\Models\User;
use App\Models\UserMedia;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\ImageExt\Models\Media;

class MediaController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '用户媒介';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new UserMedia());

        $grid->column('id', ___('table.zy_user_media.Id'));
        $grid->column('user_id', ___('table.zy_user_media.User id'))->display(function ($id){
            $user = User::find($id);
            return $user?$user->name:'---';
        });
        $grid->column('media_link', ___('table.zy_user_media.Media_link'));
        $grid->column('create_time', ___('table.zy_user_media.Create time'));
        $grid->column('update_time', ___('table.zy_user_media.Update time'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(UserMedia::findOrFail($id));

        $show->field('id', ___('table.zy_user_media.Id'));
        $show->field('user_id', ___('table.zy_user_media.User id'));
        $show->field('type', ___('table.zy_user_media.Type'));
        $show->field('media', ___('table.zy_user_media.Media'));
        $show->field('create_time', ___('table.zy_user_media.Create time'));
        $show->field('update_time', ___('table.zy_user_media.Update time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Media());

        $form->select('user_id', ___('table.zy_user_media.User id'))->options(function ($id) {
            $user = User::find($id);
            if ($user) {
                return [$user->id => $user->name];
            }
        })->ajax('/admin/ajax/user')->help("输入真实用户姓名");
        $form->imageExt('media_link', ___('table.zy_user_media.media_link'))
            ->move('user', true)
            ->type('image');
            //->help('只能上传png, jpg, jpeg, bmp, gif, webp, psd, svg, tiff');
        //$form->hidden('id');
        return $form;
    }
}
